package com.example.helloworld
import android.content.res.Configuration
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.helloworld.ui.theme.HelloWorldTheme
import java.util.Locale

class MainActivity : ComponentActivity() {
    // 【删除】原来的Activity成员变量 currentLang

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            HelloWorldTheme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
                    // 【新增】将currentLang改为Compose状态变量，用remember保存
                    var currentLang by remember { mutableIntStateOf(0) }

                    Column(
                        modifier = Modifier
                            .fillMaxSize()
                            .padding(20.dp),
                        horizontalAlignment = Alignment.CenterHorizontally,
                        verticalArrangement = Arrangement.Top
                    ) {
                        Text(
                            text = stringResource(id = R.string.hello_text),
                            fontSize = 24.sp,
                            modifier = Modifier.padding(top = 50.dp)
                        )

                        Image(
                            painter = painterResource(
                                id = when (currentLang) {
                                    0 -> R.drawable.cn_flag
                                    1 -> R.drawable.us_flag
                                    2 -> R.drawable.ru_flag
                                    else -> R.drawable.cn_flag
                                }
                            ),
                            contentDescription = "国旗",
                            modifier = Modifier
                                .size(200.dp, 150.dp)
                                .padding(top = 20.dp)
                        )

                        Button(
                            onClick = {
                                // 1. 先更新语言索引（控制国旗切换）
                                currentLang = (currentLang + 1) % 3
                                // 2. 再调用更新语言的方法（控制文字切换）
                                updateLanguage(currentLang)
                            },
                            modifier = Modifier.padding(top = 30.dp)
                        ) {
                            Text(text = stringResource(id = R.string.change_lang_btn))
                        }
                    }
                }
            }
        }
    }

    private fun updateLanguage(langCode: Int) {
        val locale = when (langCode) {
            0 -> Locale.CHINESE
            1 -> Locale.ENGLISH
            2 -> Locale("ru")
            else -> Locale.CHINESE
        }
        val config = Configuration(resources.configuration)
        config.setLocale(locale)
        // 【修改】更新配置，移除recreate()（避免页面重启）
        resources.updateConfiguration(config, resources.displayMetrics)
        // 【删除】recreate()
    }
}